package com.yuanfeng.base.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.base.dto.BasekdiniaoConfigDTO;
import com.yuanfeng.base.entity.BaseKdniaoConfigEntity;
import com.yuanfeng.base.mapper.BaseKdniaoConfigMapper;
import com.yuanfeng.base.service.BaseKdniaoConfigService;
import com.yuanfeng.commoms.config.YFConfig;
import com.yuanfeng.commoms.constant.CommonStatusEnum;
import com.yuanfeng.commoms.constant.CommonType;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.RedisUtil;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 *
 */
@Service
public class BaseKdniaoConfigServiceImpl extends ServiceImpl<BaseKdniaoConfigMapper, BaseKdniaoConfigEntity>
    implements BaseKdniaoConfigService{

    @Override
    public ResponseResult queryConfigFeign() {
            BaseKdniaoConfigEntity   kdiNiaoConfig =this.baseMapper.selectOne(new LambdaQueryWrapper<BaseKdniaoConfigEntity>()
                            .eq(BaseKdniaoConfigEntity::getStatus, CommonType.commonStatus.YES.getCode()));
            RedisUtil.set(YFConfig.KDINIAO_CONFIG, JSONObject.toJSONString(kdiNiaoConfig));
            return ResponseResult.success(kdiNiaoConfig);
    }

    @Override
    public ResponseResult updateKdiniaoConfig(BasekdiniaoConfigDTO basekdiniaoConfigReq) {
        if (null != basekdiniaoConfigReq && null != basekdiniaoConfigReq.getToken()) {
            //校验token
            String token = basekdiniaoConfigReq.getToken();
            UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(token);
            if (null != userInfo) {
                //修改快递鸟 配置
                this.baseMapper.updateKdiniaoConfig(basekdiniaoConfigReq);
                BaseKdniaoConfigEntity kdiNiaoConfig =   this.baseMapper.selectOne(new LambdaQueryWrapper<BaseKdniaoConfigEntity>()
                                                        .eq(BaseKdniaoConfigEntity::getStatus,CommonType.commonStatus.YES.getCode()));
                RedisUtil.set(YFConfig.KDINIAO_CONFIG, JSONObject.toJSONString(kdiNiaoConfig));
                return ResponseResult.success();
            }
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
        }
        return ResponseResult.fail(BizCodeEnume.VALID_EXCEPTION);
    }

    @Override
    public ResponseResult queryConfig(Map<String, Object> param) {
        String token = param.get("token").toString();
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(token);
        if (null != userInfo) {
            String kdiniaoConfigStr = RedisUtil.get(YFConfig.KDINIAO_CONFIG);
            BaseKdniaoConfigEntity kdiNiaoConfig = JSONObject.parseObject(kdiniaoConfigStr, BaseKdniaoConfigEntity.class);
            if (true) {
                 kdiNiaoConfig =   this.baseMapper.selectOne(new LambdaQueryWrapper<BaseKdniaoConfigEntity>()
                        .eq(BaseKdniaoConfigEntity::getStatus,CommonType.commonStatus.YES.getCode()));
                RedisUtil.set(YFConfig.KDINIAO_CONFIG, JSONObject.toJSONString(kdiNiaoConfig));
            }
            return ResponseResult.success(kdiNiaoConfig);
        }
        return ResponseResult.fail(CommonStatusEnum.TOKEN_ERROR.getCode(), CommonStatusEnum.TOKEN_ERROR.getValue());
    }

    @Override
    public ResponseResult queryShopKdNiao(Map<String, Object> paraMap) {
        if (paraMap.get("shopId") == null || paraMap.get("shopId").equals("")) {
           return ResponseResult.fail(2,"请求参数不能为空");
        }
        QueryWrapper<BaseKdniaoConfigEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shop_id",paraMap.get("shopId"));
        BaseKdniaoConfigEntity eqbk = this.baseMapper.selectOne(queryWrapper);
        if (eqbk == null) {
            return ResponseResult.fail(2,"查询为空！");
        }
        return ResponseResult.success(eqbk);
    }

    @Override
    public ResponseResult updatekdiniaoBaseParam(BaseKdniaoConfigEntity paraMap) {
        QueryWrapper<BaseKdniaoConfigEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shop_id",paraMap.getShopId());
        this.baseMapper.delete(queryWrapper);
        int insert = this.baseMapper.insert(paraMap);
        return ResponseResult.success(insert);
    }

    @Override
    public ResponseResult deleteShopkdniaoParam(Map<String, Object> paraMap) {
        if (paraMap.get("shopId") == null || paraMap.get("shopId").equals("")) {
            return ResponseResult.fail(2,"请求参数不能为空");
        }
        QueryWrapper<BaseKdniaoConfigEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shop_id",paraMap.get("shopId"));
        int delete = this.baseMapper.delete(queryWrapper);
        return ResponseResult.success(delete);
    }
}




